package xjtu.zd.server.common;

import xjtu.zd.server.common.action.FindNewId;
import xjtu.zd.server.common.dao.CommonDao;
import xjtu.zd.server.common.model.User.User;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Configuration
@MapperScan("xjtu.zd.server.common.dao")
@ServletComponentScan("xjtu.zd.server.common.base")

public class CommonConfig {

    private static ApplicationContext applicationContext;
    private static Environment env;

    @Autowired
    public void setApplicationContext(ApplicationContext applicationContext) {
        CommonConfig.applicationContext = applicationContext;
        CommonConfig.env = CommonConfig.applicationContext.getEnvironment();
    }


    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Environment getEnv() {
        return env;
    }

    @Bean
    public WebMvcConfigurer webMvcConfigurer() {

        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("*")
                        .allowedMethods(RequestMethod.GET.name(), RequestMethod.POST.name(), RequestMethod.PUT.name(), RequestMethod.DELETE.name());
            }
        };
    }

    @Resource
    @Bean
    public CommonService commonService( CommonDao commonDao){
        return new CommonService(){
            @Override
            public User getUserById(String userId) {
                return commonDao.findUserById(userId);
            }
            @Override
            public User getStaffById(String userId) {
                return commonDao.findStaffById(userId);
            }
            @Override
            public List<Classification> getClassification(){
                return commonDao.findClass();
            }

            @Override
            public String returnNewId(FindNewId findNewId) {
                Calendar cal = Calendar.getInstance();
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH)+1;//cal.get(Calendar.MONTH) 获取的月份从0开始
                String mon = month<10?("0"+month):(""+month);
                return year+mon+findNewId.getnewid(year+mon);
            }

            @Override
            public String getNowtime() {
                Calendar cal = Calendar.getInstance();
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date();
                return sdf.format(date);
            }
        };
    }
}
